package xpn.business.fund_return.entity.return_application_letter;

import org.springframework.context.ApplicationContextException;
import org.springframework.stereotype.Service;
import xpn.business.fund_return.entity.frcase.FrCase;
import xpn.business.fund_return.entity.frcase.FrCaseDao;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class ReturnApplicationLetterService {
    @Resource
    private ReturnApplicationLetterDao returnApplicationLetterDao;

    @Resource
    private FrCaseDao frCaseDao;

//    @Resource
//    private FrCaseService frCaseService;

    public ReturnApplicationLetter save(ReturnApplicationLetter returnApplicationLetter) {
        if (Objects.isNull(returnApplicationLetter.getId())) {
            ReturnApplicationLetter exist = returnApplicationLetterDao.findAllByCaseId(returnApplicationLetter.getCaseId());
            if (Objects.nonNull(exist)) {
                throw new ApplicationContextException("该表单已存在，请勿重复创建");
            }
        }
        FrCase frCase = frCaseDao.getOne(returnApplicationLetter.getCaseId());
        if(Objects.nonNull(returnApplicationLetter.getCaseName()) && !returnApplicationLetter.getCaseName().equals(""))
            frCase.setCaseCodeCommon(returnApplicationLetter.getCaseName());
        frCase.setCaseNature(returnApplicationLetter.getCaseNature());
        frCase.setRefundAmount(returnApplicationLetter.getRefundAmount());
        frCaseDao.save(frCase);
//        frCaseService.changeWriteStatus(returnApplicationLetter.getCaseId(), CaseWriteStatus.Letter);
        return returnApplicationLetterDao.save(returnApplicationLetter);
    }
}
